$(document).ready(function() {
	
	$body = $('.body');
	$body.wrapInner('<div class="dummy" />');	
	$body.prepend('<div class="imgaid" />');
	$imgaid = $('.imgaid');
	
	preloadImages();
	
	var firstimg = $('.gallery a').first().attr("href");
	$body.css("backgroundImage", "url(" + firstimg + ")"); 
	$imgaid.css("backgroundImage", "url(" + firstimg + ")"); 
	
	function preloadImages(){
		imageObj = new Image();
		var images = $('.gallery a').each(function(index) {imageObj.src = $(this).attr('href');});
	}
	
	$imgaid.width($body.width());
	$imgaid.height($body.height());

	
	function chooseImage(){
		if($('.gallery li.active').next().size() == 0) $('.gallery a').first().click();
		else $('.gallery li.active').next().children().first().click();
	}
	
	setInterval(chooseImage,5000);
	
	$gallery = $('.gallery ul > li');
	$gallery.css('opacity',0.75);
	$gallery.mouseover(function() {
		$(this).animate(
				{opacity:1.0},
				{duration: 300}
				);
		});
	$gallery.mouseout(function() {
		$(this).animate(
				{opacity: 0.6}, 
				{duration: 300}
				);
		});
	

	$(".gallery a").click(function(event) {
		event.preventDefault();
		$(".gallery li.active").css('opacity','0.6').removeClass('active');
		$(this).parent().addClass('active').css('opacity','1');
		$body.css("backgroundImage", "url(" + $(this).attr("href") + ")"); 
		$imgaid.fadeOut(600, function() {$imgaid.css("backgroundImage",$body.css("backgroundImage"));$imgaid.show();});
	});
	
	var api = $('.scrollbox').data('jsp');
	var scroll = false;
	var speed = 90;
	var step = 25;
	
	$up = $('#sUp');
	$down = $('#sDown');
	$up.live('mousedown',function() {scroll = true;scrollUp();});
	$up.live('mouseup',function(){scroll=false;});
	$down.live('mousedown',function() {scroll = true;scrollDown();});
	$down.live('mouseup',function(){scroll=false;});
		
	function scrollDown(){
		if(api.getPercentScrolledY() < 99){api.scrollByY(step);}else{api.scrollToY(api.getContentHeight());scroll = false;}
		if(scroll == true)
			setTimeout(scrollDown,speed);
	}
	function scrollUp(){
		if(api.getContentPositionY() > step){api.scrollByY(-1 * step);}else{api.scrollToY(0);scroll = false;}
		if(scroll == true)
			setTimeout(scrollUp,speed);
	}
	
	$('.logo').pngFix();
	$('.slideshow').livequery(function(){$('.slideshow').nivoSlider({effect:'fade',directionNav:false,controlNav:false});});
    $('.scrollbox').livequery(function(){
    	$('.scrollbox').jScrollPane({animateScroll: true});api = $('.scrollbox').data('jsp');
    	$('.scrollbox').css('overflow','auto');
    	});
    $("a[rel^='side']").livequery(function(){$("a[rel^='side']").prettyPhoto({theme: 'dark_rounded'});});
    $("a[rel^='prettyPhoto']").livequery(function(){$("a[rel^='prettyPhoto']").prettyPhoto({theme: 'dark_rounded'});});
     
	  var newHash      = "",
      $mainContent = $(".body-wrapper"),
      $pageWrap    = $(".body"),
      baseHeight   = 0,
      $el;

	  $("a.reacties").live('click',function(e){
		  e.preventDefault();
		  var id = parseInt($(this).attr('index'));
		  $("#entry_"+id+" .comments").toggle();
		  api.reinitialise();
	  });
	  
	 // $('.scrollbox').resize(function(){alert('test');});
	  

	  $("nav a").mouseover(function() {$(this).parent().addClass("hover");}).mouseout(function(){$(this).parent().removeClass("hover");});
	  
	 
	  $.prettyLoader({
			animation_speed: 'fast', /* fast/normal/slow/integer */
			bind_to_ajax: true, /* true/false */
			delay: false, /* false OR time in milliseconds (ms) */
			loader: 'http://www.opur.be/popshouse/images/layout/prettyLoader/ajax-loader.gif', /* Path to your loader gif */
			offset_top: -8, /* integer */
			offset_left: 10 /* integer */
		});
	  
	  var _gaq = _gaq || [];
	  _gaq.push(['_setAccount', 'UA-22428379-1']);
	  _gaq.push(['_trackPageview']);

	  (function() {
	    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	  })();
	  
	  var History = window.History,
	  				State = History.getState();
	  
	  /*History.Adapter.bind(window,'statechange',function(){
			var State = History.getState();
			//History.log('statechange:', State.data, State.title, State.url);
		});*/
	  
	  $("a[rel^='ajax']").live('click', function(e) {
		  //window.location.hash = $(this).attr("href");return false;
		  e.preventDefault();
		  History.pushState(null, $(this).text(), $(this).attr('href'));
		  });
	  
	  $(window).bind('statechange', function(){
	     // newHash = window.location.hash.substring(1);
		  State = History.getState();
		  newHash = url = State.url;
		  
          $("nav li").removeClass("current");
	      if (newHash && newHash.search("nieuws") == -1) {
	    	  
	    	  if(!$('.nieuws').is(':hidden')){
	    		  $('#nieuws-handle').click();
	    	  }
	    		
	    		//$.prettyLoader.show();
	    		
	    		$mainContent
	              .find(".content")
	              .slideUp(300, function() {
	            	  
	            	  $('.pageLoad').remove();
	          		  $('.container').append('<span class="pageLoad">LOADING...</span>');
	          	      $('.pageLoad').fadeIn('normal');
	                  $mainContent.hide().load(newHash + " .content", function() {
	                	  $('.pageLoad').fadeOut('normal');
	                      $mainContent.slideDown(300, function() {});

	                      $("nav a[href='"+newHash+"']").parent().addClass("current");
	                      
	                      //var hash = window.location.hash.replace('#','') || '';
	                      //var query_string = window.location.search || '';
	                     // alert(hash);
	                      //_gaq.push(['_trackPageview',location.pathname + query_string  + hash]);
	                      //_gaq.push(['_trackPageview',hash]);
	                  });
	              });
	      };
	
	  });
	  
	  $(window).trigger('statechange');
	
	  $('#nieuws-handle').click(function(e){
		  e.preventDefault();
		  $('.nieuws').slideToggle(300, function(){

			  if($('.nieuws').is(':hidden')){
				  opac = 1;
				  $('.nieuws-handle-arrow').html("&#x21E9;");
			  }else{
				  opac = 0.3;
				  $('.nieuws-handle-arrow').html("&#x21E7;");
			  }
			  
			  $('.content').animate({
				    opacity: opac
				  }, 400, function() {});
		  });
		  
	  });
	  

	  $.tools.tabs.addEffect("slide", function(i, done) {
	  	this.getPanes().slideUp();
	  	this.getPanes().eq(i).slideDown(function()  {
	  		$(this).css({backgroundColor: 'transparent'});
	  		done.call();
	  	});
	  });
	  
	  //TODO $("ul.tabs").tabs("div.panes > div", {effect: 'slide', initialIndex: null, rotate: true}).slideshow({autoplay: true, interval: 7000});

	
});